package cn.classTest.mall.controller;

import cn.classTest.mall.domain.Department;
import cn.classTest.mall.domain.Employee;
import cn.classTest.mall.domain.DataMag;
import cn.classTest.mall.query.EmployeeQueryObject;
import cn.classTest.mall.service.DepartmentService;
import cn.classTest.mall.service.EmployeeService;
import cn.classTest.mall.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.management.relation.Role;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeServiceImpl;

    @Autowired
    private DepartmentService departmentServiceImpl;

    @Autowired
    private RoleService roleServiceImpl;

    @RequiresPermissions(value = {"employee:list", "员工列表"}, logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeServiceImpl.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<Department> departments = departmentServiceImpl.selectAll();
        model.addAttribute("departments", departments);
        return "/employee/list";
    }
    @RequiresPermissions(value = {"employee:saveOrUpdate", "员工新增或修改"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public DataMag saveOrUpdate(Employee employee, Long[] roleIds, Model model) {
        DataMag dataMsg = new DataMag();
        try {
            employeeServiceImpl.saveOrUpdate(employee, roleIds);
            dataMsg.setSuccess(true);
            return dataMsg;
        } catch (Exception exception) {
            exception.printStackTrace();
            dataMsg.setSuccess(false);
            dataMsg.setMsg("保存失败");
            model.addAttribute("ex", exception);
            return dataMsg;
        }
    }
    @RequiresPermissions(value = {"employee:delete", "员工删除"}, logical = Logical.OR)
    @RequestMapping("/delete")
    public String delete(Long id) {
        employeeServiceImpl.delete(id);
        return "redirect:/employee/list";
    }
    @RequiresPermissions(value = {"employee:input", "员工编辑"}, logical = Logical.OR)
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Employee employee = employeeServiceImpl.selectOne(id);
            model.addAttribute("employee", employee);
        }
        List<Department> departments = departmentServiceImpl.selectAll();
        model.addAttribute("departments", departments);
        List<Role> roles = roleServiceImpl.selectAll();
        model.addAttribute("roles", roles);
        return "/employee/input";
    }
}
